package com.example.file_management.mapper.material;

import com.example.file_management.pojo.material.Resource;
import com.example.file_management.pojo.material.ResourceTypeStats;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ResourceMapper {

    /**
     * 📌 统计不同资源类型的文件总大小和最近上传时间
     */
    @Select("SELECT resource_type, SUM(size) AS total_size, MAX(upload_time) AS last_upload " +
            "FROM resourcetable GROUP BY resource_type")
    @Results({
            @Result(property = "resourceType", column = "resource_type"),
            @Result(property = "size", column = "total_size"),
            @Result(property = "uploadTime", column = "last_upload")
    })
    List<ResourceTypeStats> getCategoryStatistics();

    @Select("SELECT task_id FROM task_table WHERE task_name = #{taskName}")
    Long selectTaskIdByTaskName(@Param("taskName") String taskName);

    /**
     * 📌 插入资源
     */
    @Insert("INSERT INTO resourcetable (task_id, task_name, resource_name, resource_type, file_path, upload_time, version, size) " +
            "VALUES (#{taskId}, #{taskName}, #{resourceName}, #{resourceType}, #{filePath}, #{uploadTime}, #{version}, #{size})")
    @Options(useGeneratedKeys = true, keyProperty = "resourceId")
    int insertDocument(Resource resource);

    /**
     * 📌 通过文件路径查询资源
     */
    @Select("SELECT * FROM resourcetable WHERE file_path = #{filePath}")
    @Results({
            @Result(property = "resourceId", column = "ResourceID"),
            @Result(property = "taskId", column = "task_id"),
            @Result(property = "resourceName", column = "resource_name"),
            @Result(property = "taskName", column = "task_name"),
            @Result(property = "resourceType", column = "resource_type"),
            @Result(property = "filePath", column = "file_path"),
            @Result(property = "uploadTime", column = "upload_time"),
            @Result(property = "version", column = "version"),
            @Result(property = "size", column = "size")
    })
    Resource getByFilePath(String filePath);

    /**
     * 📌 查询某任务下的所有文件（按上传时间倒序）
     */
    @Select("SELECT * FROM resourcetable WHERE task_name = #{taskName} ORDER BY upload_time DESC")
    @Results({
            @Result(property = "resourceId", column = "resource_id"),
            @Result(property = "taskName", column = "task_name"),
            @Result(property = "resourceName", column = "resource_name"),
            @Result(property = "resourceType", column = "resource_type"),
            @Result(property = "filePath", column = "file_path"),
            @Result(property = "uploadTime", column = "upload_time"),
            @Result(property = "version", column = "version"),
            @Result(property = "size", column = "size")
    })
    List<Resource> getResourcesByTaskName(@Param("taskName") String taskName);

    /**
     * 📌 查询文件的最新版本号
     */
    @Select("SELECT COALESCE(MAX(version), 0) FROM resourcetable " +
            "WHERE task_id = #{taskId} AND resource_name = #{fileName}")
    int getLatestVersion(@Param("taskId") Long taskId, @Param("fileName") String fileName);

    /**
     * 📌 查询指定文件的所有版本（按版本号倒序）
     */
    @Select("SELECT * FROM resourcetable WHERE task_name = #{taskName} " +
            "AND resource_name = #{fileName} ORDER BY version DESC")
    @Results({
            @Result(property = "resourceId", column = "resource_id"),
            @Result(property = "taskName", column = "task_name"),
            @Result(property = "resourceName", column = "resource_name"),
            @Result(property = "resourceType", column = "resource_type"),
            @Result(property = "filePath", column = "file_path"),
            @Result(property = "uploadTime", column = "upload_time"),
            @Result(property = "version", column = "version"),
            @Result(property = "size", column = "size")
    })
    List<Resource> getFileVersions(@Param("taskName") String taskName, @Param("fileName") String fileName);

    /**
     * 📌 版本回滚：创建新记录，版本号 +1
     */
    @Insert("INSERT INTO resourcetable (task_name, resource_name, resource_type, file_path, upload_time, version, size) " +
            "SELECT task_name, resource_name, resource_type, file_path, NOW(), " +
            "(SELECT COALESCE(MAX(version), 0) + 1 FROM resourcetable " +
            "WHERE task_name = #{taskName} AND resource_name = #{fileName}), size " +
            "FROM resourcetable WHERE task_name = #{taskName} " +
            "AND resource_name = #{fileName} AND version = #{targetVersion}")
    int rollbackToVersion(@Param("taskName") String taskName, @Param("fileName") String fileName, @Param("targetVersion") int targetVersion);

    /**
     * 📌 删除指定版本的文件（保持原删除功能不变）
     */
    @Delete("DELETE FROM resourcetable WHERE task_name = #{taskName} " +
            "AND resource_name = #{fileName} AND version = #{version}")
    int deleteSpecificVersion(@Param("taskName") String taskName, @Param("fileName") String fileName, @Param("version") int version);

    /**
     * 📌 删除某个文件的所有版本（保持原删除功能不变）
     */
    @Delete("DELETE FROM resourcetable WHERE task_name = #{taskName} " +
            "AND resource_name = #{fileName}")
    int deleteAllVersions(@Param("taskName") String taskName, @Param("fileName") String fileName);

    /**
     * 📌 根据文件名称查询文件
     */
    @Select("SELECT * FROM resourcetable WHERE resource_name = #{fileName}")
    @Results({
            @Result(property = "resourceId", column = "resource_id"),
            @Result(property = "taskName", column = "task_name"),
            @Result(property = "resourceName", column = "resource_name"),
            @Result(property = "resourceType", column = "resource_type"),
            @Result(property = "filePath", column = "file_path"),
            @Result(property = "uploadTime", column = "upload_time"),
            @Result(property = "version", column = "version"),
            @Result(property = "size", column = "size")
    })
    List<Resource> getResourcesByFileName(@Param("fileName") String fileName);
}
